/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com 

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

//Include library header

#include <iostream>

#include "filebrowser.h"

namespace tri{
namespace ui{

FileSystemBrower::FileSystemBrower() : tri::ui::extPopup(),mDirChanged(true),mFilterExtension(""){
	setSize(500,300,0);
	setStyle("");
	setColor(0,0,0,200);
	setup();
	reset();
}
FileSystemBrower::~FileSystemBrower(){}

void FileSystemBrower::reset(){
	setDirectory(tri::sys::getcwdir());
}

void FileSystemBrower::moveUpOneLevel(){
	std::string dir = tri::sys::getParentDirectory(mCurDir);
	setDirectory(dir);
}

void FileSystemBrower::setDirectory(std::string dir){
	if (tri::sys::isdir(dir)){
		mCurDir = dir;
		updateFileList();
	}
}


void FileSystemBrower::setFilterExtension(std::string str){
	mFilterExtension = str;
	updateFileList();
}


void FileSystemBrower::onItemClicked(tri::ui::extItemList* list){
	if(list!=NULL){
		std::string item =list->getSelectedItem();
		if(item == ".."){
			moveUpOneLevel();
		}else if(item == "."){
			
		}else if(tri::sys::isdir(tri::sys::join(mCurDir,item))){
			setDirectory(tri::sys::join(mCurDir,item));
		}else if (tri::sys::isfile(tri::sys::join(mCurDir,item))){
			mFileNameEdit.setText(item.c_str());
		}
	}
}

void FileSystemBrower::updateFileList(){
	mFileListPanel.clear();
	mDirectoryNameEdit.setText(mCurDir.c_str());
	
	std::vector<std::string> folders;
	if (tri::sys::getdirs(mCurDir,folders)!=-1){	
		for (int i=0;i<folders.size();i++){
			mFileListPanel.addItem(folders[i],"data\\ui\\win7\\icons\\folder_icon.png");
		}
	}
	
	std::vector<std::string> files;
	std::vector<std::string> ext;
	if (mFilterExtension != ""){
		ext.push_back(mFilterExtension);
	}
	if (tri::sys::get_files(mCurDir,files,ext)!=-1){
		for (int i=0;i<files.size();i++){
			if(tri::sys::has_extension(files[i],".exe")){
				mFileListPanel.addItem(files[i],"data\\ui\\win7\\icons\\app_icon.png");
			}else{
				mFileListPanel.addItem(files[i],"data\\ui\\win7\\icons\\file_icon.png");
			}
		}
	}
	
	mFileListPanel.pack();
	mFilePanelScrollBox.pack();
	mFilePanelScrollBox.scrollToTop();
	mFilePanelScrollBox.scrollToLeft();
}

void FileSystemBrower::onOk(){
	this->hide();
}

void FileSystemBrower::onCancel(){
	this->hide();
}


std::string FileSystemBrower::getDirectory(){
	return mCurDir;
}

std::string FileSystemBrower::getFullFilePath(){
	return tri::sys::join(mCurDir,mFileNameEdit.getText());
}

std::string FileSystemBrower::getFile(){
	return mFileNameEdit.getText();
}

void FileSystemBrower::setup(){
	addComponent(&mBackPanel);
	mBackPanel.setSize(500,300,0);
	
	
	//==============================
	mBackPanel.addComponent(&mDirectoryNameEdit);
	mDirectoryNameEdit.setSize(400,24,0);
	mDirectoryNameEdit.setPosition(0,130,0);
	
	//================================
	
	std::string style_name = "FileSystemBrower.UpOneLevelButtonStyle";
	tri::ui::extStyle::createStyle(style_name);
	tri::ui::extStyle* style = tri::ui::extStyle::getStyle(style_name);
	style->clear();
	tri::ui::extUIStyle uiStyle;
	uiStyle.loadButtonStyle(style);
	style->setBGImage("data\\ui\\win7\\icons\\up_one_level.png");
	style->setBGImage("data\\ui\\win7\\icons\\up_one_level_mo.png",tri::MOUSE_OVER);
	style->setBGImage("data\\ui\\win7\\icons\\up_one_level_mo.png",tri::PRESSED);
	style->setBGImage("data\\ui\\win7\\icons\\up_one_level_mo.png",tri::FOCUS);
	
	mBackPanel.addComponent(&mUpOneLevelButton);
	mUpOneLevelButton.setSize(24,24,0);
	mUpOneLevelButton.setPosition(215,130,0);
	mUpOneLevelButton.CLICKED.connect(this,&FileSystemBrower::moveUpOneLevel);
	mUpOneLevelButton.setStyle(style_name);
	
	//================================
	mBackPanel.addComponent(&mOkButton);
	mOkButton.setSize(80,24,0);
	mOkButton.setPosition(-90,-130,0);
	mOkButton.setText("Ok");
	mOkButton.CLICKED.connect(this,&FileSystemBrower::onOk);
	
	//================================
	mBackPanel.addComponent(&mCancelButton);
	mCancelButton.setText("Cancel");
	mCancelButton.setSize(80,24,0);
	mCancelButton.setPosition(90,-130,0);
	mCancelButton.CLICKED.connect(this,&FileSystemBrower::onCancel);
	
	//==============================
	mBackPanel.addComponent(&mFileNameEdit);
	mFileNameEdit.setSize(400,24,0);
	mFileNameEdit.setPosition(0,-100,0);
	
	//================================
	mBackPanel.addComponent(&mFilePanelScrollBox);
	mFilePanelScrollBox.setSize(400,190,0);
	mFilePanelScrollBox.setPosition(0,15,0);
	
	//================================
	mFilePanelScrollBox.setContent(&mFileListPanel);
	mFileListPanel.setSize(396,190,0);
	mFileListPanel.setIconSize(16);
	mFileListPanel.SELECTION_CHANGE.connect(this,&FileSystemBrower::onItemClicked);
} 

}
}